/*
day251
2020.5.5
*/ 
#include<vector>
#include<cstdlib>
using namespace std;

struct TreeNode {
	int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class Solution {
public:
    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
    	int index=0;
		return help(preorder,inorder,index,0,preorder.size()-1);
    }
    
    TreeNode *help(vector<int>& preorder, vector<int>& inorder,int &index,int l,int r)
    {
    	if(index>=preorder.size() || l>r)
    	{
    		return nullptr;
		}
		
		
		int val=preorder[index];
		
		
		int tem=l;
		while(tem<=r && inorder[tem]!=val)
		{
			++tem;
		}
		if(tem>r)
		{
			return nullptr;
		}
		 
		index++;
		TreeNode *root=new TreeNode(val);
		
		root->left=help(preorder,inorder,index,l,tem-1);
		root->right=help(preorder,inorder,index,tem+1,r);
		
		return root;
	}
    
};

int main()
{
	return 0;
}
